
package com.haizhi.oa;

import java.awt.image.RescaleOp;

import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.eclipse.paho.client.mqttv3.internal.MemoryPersistence;

import com.google.gson.Gson;
import com.haizhi.oa.MqttMessageModel.SendMessageModel;


public class MqttClientUtil implements MqttCallback{
	private static final String MESSAGE_ADD = "messages/add";
    private MqttClient client;
    private String me_id;
    private Gson gson = new Gson();
    public MqttClientUtil (String me_id,String url, String topic, int qos, String clientID,String username,String password){    
        try {
            this.client = new MqttClient(url, clientID,new MemoryPersistence());
            this.me_id = me_id;
            MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
            mqttConnectOptions.setCleanSession(true);
            mqttConnectOptions.setKeepAliveInterval(15);
            mqttConnectOptions.setConnectionTimeout(10000);
            mqttConnectOptions.setUserName(username);
            mqttConnectOptions.setPassword(password.toCharArray());
            this.client.setCallback(this);     
            this.client.connect(mqttConnectOptions);
            this.client.subscribe(topic);
            this.client.subscribe(MESSAGE_ADD);
        } catch (MqttException ex) {
            ex.printStackTrace();
        }
    }
    public void sendMessage(String targetId,String msg) {
		final MqttTopic mqttTopic = client.getTopic(MESSAGE_ADD);
		MqttMessage message = new MqttMessage(createMessage(targetId, msg).getBytes());
		try {
			mqttTopic.publish(message);
		} catch (MqttException e) {
			e.printStackTrace();
		}
	}
	public void disconnect() {
		try {
			client.disconnect();
		} catch (MqttException e) {
			e.printStackTrace();
		}
	}
	private String createMessage(String targetid,String msg){
		ChatContent content = new ChatContent();
        content.text = msg;
        content.type = "0";
        MqttMessageModel messageModel = new MqttMessageModel();
        messageModel.action = MESSAGE_ADD;
        SendMessageModel sModel = new MqttMessageModel.SendMessageModel();
        sModel.uuid = System.currentTimeMillis() + "";
        sModel.tenantId = "1182";
        sModel.createdAt = System.currentTimeMillis() + "";
        sModel.sourceId = me_id;
        sModel.targetId = targetid;
        sModel.targetType = "2";
        sModel.contentType = "0";
        sModel.content = gson.toJson(content);
        messageModel.content = sModel;
        String res = gson.toJson(messageModel);
        return res;
	}
    
	@Override
	public void connectionLost(Throwable error) {
		System.err.println(error.getMessage());
	}
	@Override
	public void deliveryComplete(MqttDeliveryToken token) {
		
	}
	@Override
	public void messageArrived(MqttTopic topic, MqttMessage message) throws Exception {
		String msg = new String(message.getPayload());
		System.err.println("[topic:"+topic.getName()+"]\n"+msg);
	}
    
}
